﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DiBang.Common;
using DiBang.Sql;
using System.IO;

namespace ChangWuDiBang
{
    public partial class FrmDiBangEdit : Office2007Form
    {
        public UserInfo _UserInfo
        {
            get;
            set;
        }

        public DiBangInfo _DiBangInfo
        {
            get;
            set;
        }

        SqlLib sqllib = new SqlLib();
        SqlExtend sqlextend = new SqlExtend();

        public FrmDiBangEdit()
        {
            this.EnableGlass = false;
            InitializeComponent();
        }

        private void frmDiBangEdit_Load(object sender, EventArgs e)
        {
            if (_UserInfo == null || _DiBangInfo == null)
            {
                return;
            }

            //
            txtUserID.Text = _UserInfo.UserID;
            //
            lblVDate.Text = _DiBangInfo.ChengZhongDate;
            lblVDiBangID.Text = _DiBangInfo.Dibang_ID;
            txtPinMing.Text = _DiBangInfo.PinMing;
            txtCheHao.Text = _DiBangInfo.CheHao;
            txtOther.Text = _DiBangInfo.ChengZhongOther;

            txtMaoZhong.Text=_DiBangInfo.MaoZhong;
            txtPiZhong.Text=_DiBangInfo.PiZhong;
            txtRongQiNum.Text=_DiBangInfo.RongQiNum;
            txtRongQiZhong.Text=_DiBangInfo.RongQiZhong;
            txtJingZhong.Text=_DiBangInfo.JingZhong;

            //获取图片
            string image_id = _DiBangInfo.ImageID;
            if (!string.IsNullOrEmpty(image_id))
            {
                string ssql = @"SELECT FIRST_PHOTO,SECOND_PHOTO FROM DB_IMAGE WHERE IMAGE_ID='" + image_id + "'";
                DataTable dtImage = sqllib.GetDataSet(ssql).Tables[0];
                byte[] byteFirstPic = null;
                if (dtImage != null && dtImage.Rows.Count > 0)
                {
                    byteFirstPic = (byte[])dtImage.Rows[0]["FIRST_PHOTO"];
                }

                if (byteFirstPic != null)
                {
                    //把Byte数组转换成Image
                    MemoryStream msFirst = new MemoryStream(byteFirstPic);
                    Image imageFirst = Image.FromStream(msFirst, true);
                    msFirst.Dispose();
                    picFirst.Image = imageFirst;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPinMing.Text.Trim()))
            {
                MessageBoxEx.Show("品名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPinMing.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtCheHao.Text.Trim()))
            {
                MessageBoxEx.Show("车号不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCheHao.Focus();
                return;
            }

            UserInfo tempUserInfo = sqlextend.GetUserInfo(txtUserID.Text);
            if (tempUserInfo.UserGroup != ConstParam.UserGroup_Administrator
               && tempUserInfo.UserGroup != ConstParam.UserGroup_User)
            {
                MessageBoxEx.Show("没有修改的权限,请确认!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (!sqlextend.UserPwdCheck(txtUserID.Text, txtPwd.Text))
            {
                MessageBoxEx.Show("账户或密码不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string lasteventtimekey = sqlextend.GetTimeKey();
            Dictionary<string, object> setMap = new Dictionary<string, object>();
            setMap.Clear();
            setMap.Add("CHE_HAO", txtCheHao.Text.Trim());
            setMap.Add("PIN_MING", txtPinMing.Text.Trim());
            setMap.Add("OTHER", txtOther.Text.Trim());

            setMap.Add("RONGQINUM", txtRongQiNum.Text.Trim());
            setMap.Add("RONGQIZHONG", Base64Encoding.BaseEncoding(txtRongQiZhong.Text.Trim()));//需要加密存储
            setMap.Add("JINGZHONG",Base64Encoding.BaseEncoding(txtJingZhong.Text.Trim()));//需要加密存储

            setMap.Add("LAST_EVENT_NAME", "修改");
            setMap.Add("LAST_EVENT_USER_ID", txtUserID.Text);
            setMap.Add("LAST_EVENT_TIMEKEY", lasteventtimekey);

            Dictionary<string, object> whereMap = new Dictionary<string, object>();
            whereMap.Clear();
            whereMap.Add("DIBANG_ID", int.Parse(lblVDiBangID.Text));

            if (sqllib.UpdateTable("DB_DIBANG", setMap, whereMap) > -1)
            {
                sqlextend.MakeDiBangHistory(lasteventtimekey);//插入履历
                MessageBoxEx.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtRongQiZhong_TextChanged(object sender, EventArgs e)
        {
            int iMaoZhong = 0;
            int iPiZhong = 0;
            int iRongQiZhong = 0;

            int.TryParse(txtMaoZhong.Text.Trim(), out iMaoZhong);
            int.TryParse(txtPiZhong.Text.Trim(), out iPiZhong);
            int.TryParse(txtRongQiZhong.Text.Trim(), out iRongQiZhong);

            txtJingZhong.Text = (iMaoZhong - iPiZhong - iRongQiZhong).ToString();
        }

        private void txtRongQiNum_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(txtRongQiNum.Text.Trim()))
                {
                    int iRongQiNum = int.Parse(txtRongQiNum.Text.Trim());
                    int iAveRongQi = int.Parse(ConstParam.AveRongQi);
                    txtRongQiZhong.Text = (iRongQiNum * iAveRongQi).ToString();
                }
                else
                {
                    txtRongQiZhong.Clear();
                }
            }
            catch
            {
                MessageBoxEx.Show("输入的数字不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtRongQiNum.Clear();
            }
        }
    }
}
